package com.roundarchisobar.njariwala.netflix.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.roundarchisobar.njariwala.netflix.dto.NetflixCatalogTitle;
import com.roundarchisobar.njariwala.netflix.dto.NetflixCatalogTitles;
import com.roundarchisobar.njariwala.netflix.dto.NetflixPeople;
import com.roundarchisobar.njariwala.netflix.dto.NetflixRequestParam;
import com.roundarchisobar.njariwala.netflix.dto.elements.Link;
import com.roundarchisobar.njariwala.netflix.request.SearchRequest;
import com.roundarchisobar.njariwala.netflix.service.AbstractNetflixService;
import com.roundarchisobar.njariwala.netflix.service.NetflixSearchService;

@Component
public class NetflixSearchServiceImpl extends AbstractNetflixService implements NetflixSearchService {

	private static final Logger logger = LoggerFactory.getLogger(NetflixSearchServiceImpl.class);

	@Override
	public NetflixPeople searchPeople(String term) {
		List<NetflixRequestParam> netflixRequestParams = getInitialRequestList();
		netflixRequestParams.add(new NetflixRequestParam(MAX_RESULTS, Integer.toString(DEFAULT_MAX_RESULTS)));
		netflixRequestParams.add(new NetflixRequestParam(TERM, term));
		String signature = createSignature(netflixRequestParams, CATALOG_PEOPLE);
		netflixRequestParams.add(new NetflixRequestParam(OAUTH_SIGNATURE, signature));
		NetflixPeople netflixPeople = (NetflixPeople) makeCall(netflixRequestParams, NetflixPeople.class,
				CATALOG_PEOPLE);
		return netflixPeople;
	}

	@Override
	public NetflixCatalogTitles searchTitle(SearchRequest searchRequest) {
		List<NetflixRequestParam> netflixRequestParams = getInitialRequestList();
		netflixRequestParams.add(new NetflixRequestParam(MAX_RESULTS, Long.toString(searchRequest.getMaxResult())));
		netflixRequestParams.add(new NetflixRequestParam(START_INDEX, Long.toString(searchRequest.getPageNum()
				* searchRequest.getMaxResult())));
		netflixRequestParams.add(new NetflixRequestParam(TERM, searchRequest.getTerm()));
		String signature = createSignature(netflixRequestParams, CATALOG_TITLE);
		netflixRequestParams.add(new NetflixRequestParam(OAUTH_SIGNATURE, signature));
		NetflixCatalogTitles netflixCatalogTitles = (NetflixCatalogTitles) makeCall(netflixRequestParams,
				NetflixCatalogTitles.class, CATALOG_TITLE);

		for (NetflixCatalogTitle netflixCatalogTitle : netflixCatalogTitles.getNetflixCatalogTitles()) {
			for (Link link : netflixCatalogTitle.getLinks()) {
				if (StringUtils.equals(link.getTitle(), "synopsis")) {
					netflixRequestParams = getInitialRequestList();
					signature = createSignature(netflixRequestParams, link.getHref());
					netflixRequestParams.add(new NetflixRequestParam(OAUTH_SIGNATURE, signature));
					String netflixSynopsis =  (String)makeCallForStringResponse(netflixRequestParams,link.getHref());
					netflixCatalogTitle.setSynopsis(netflixSynopsis);
				}
			}
		}
		return netflixCatalogTitles;
	}

	@Override
	public NetflixCatalogTitle getVideoDetails(String movieSeriesId, String movieSeriesType) {
		List<NetflixRequestParam> netflixRequestParams = getInitialRequestList();
		StringBuilder requestURL = new StringBuilder(CATALOG_TITLE).append("/").append(movieSeriesType).append("/")
				.append(movieSeriesId);
		String signature = createSignature(netflixRequestParams, requestURL.toString());
		netflixRequestParams.add(new NetflixRequestParam(OAUTH_SIGNATURE, signature));
		NetflixCatalogTitle netflixCatalogTitle = (NetflixCatalogTitle) makeCall(netflixRequestParams,
				NetflixCatalogTitle.class, requestURL.toString());

		return netflixCatalogTitle;
	}

	public List<String> getSearchTypeList() {
		List<String> searchTypes = new ArrayList<String>();
		searchTypes.add("title");
		searchTypes.add("people");
		return searchTypes;
	}
}
